package com.cash.channel.svc.service.common;

import com.alibaba.fastjson.JSON;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.exception.CashChannelException;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.request.RepayStatusRequest;
import com.cash.channel.api.model.response.RepayStatusResponse;
import com.cash.channel.api.model.user.ThirdIdModel;
import com.cash.channel.svc.logic.ThirdCommonLogic;
import com.cash.channel.svc.service.base.RepayStatus;
import com.google.common.collect.Maps;
import geex.trans.enums.RepayOnLineTypeEnum;
import geex.trans.req.DeductResultReq;
import geex.trans.rsp.DeductResultRsp;
import geex.trans.service.TransDeductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Slf4j
@Service(Constant.CHANNEL.COMMON+ Constant.SERVICE.查询还款结果)
public class CommonRepayStatus implements RepayStatus {

    @Autowired
    private ThirdCommonLogic thirdCommonLogic;

    @Autowired
    private TransDeductService transDeductService;

    @Override
    public ApiResponseModel<RepayStatusResponse> getRepayStatus(ApiRequestModel apiRequestModel) {
        try {
            RepayStatusRequest repayStatusRequest = JSON.parseObject(apiRequestModel.getData(), RepayStatusRequest.class);

            DeductResultReq deductResultReq = new DeductResultReq();
            if (StringUtils.isEmpty(repayStatusRequest.getExtId())) {
                deductResultReq.setAppId(repayStatusRequest.getWithdrawId());
                deductResultReq.setTenor(repayStatusRequest.getPeriod());
                deductResultReq.setRepayType(RepayOnLineTypeEnum.STANDARD);
            } else {
                deductResultReq.setExtId(repayStatusRequest.getExtId());
            }
            log.error("请求资金端："+ JSON.toJSONString(deductResultReq));
            DeductResultRsp deductResultRsp = transDeductService.deductResult(deductResultReq);
            log.error("请求资金端："+JSON.toJSONString(deductResultRsp));
            if (!deductResultRsp.isSuccess()) {
                log.error("请求资金端失败："+JSON.toJSONString(deductResultRsp));
                return new ApiResponseModel<>(false ,-1, deductResultRsp.getResMsg(), null);
            }
            Integer DeductType = 10;
            if(deductResultRsp.getDeductResultEnum()!=null){
                DeductType = deductResultRsp.getDeductResultEnum().get();
            }
            Long time =null;
            if(deductResultRsp.getRepayDate()!=null){
                time = deductResultRsp.getRepayDate().getTime();
            }
            return new ApiResponseModel<>(true, 0, "", new RepayStatusResponse(DeductType, deductResultRsp.getResMsg(),time));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new ApiResponseModel<>(false, -1, e.getMessage(), null);
        }
    }
}
